package pl.edu.pk.mech.kkbus.controller;

import java.util.List;
import java.util.logging.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import pl.edu.pk.mech.kkbus.service.WorkPlanService;
import pl.edu.pk.mech.kkbus.dto.WorkPlan;
import pl.edu.pk.mech.kkbus.model.ResponseStatus;

@Controller
public class WorkPlanController extends RestController {
 
    @Autowired
    WorkPlanService workPlanService;
    
    private static final Logger log = Logger.getLogger(WorkPlanController.class.getName());
    
    @RequestMapping(value = "/workplans", method = RequestMethod.GET)
    public @ResponseBody List<WorkPlan> getAllWorkPlan() {
        log.info("Get all work plan");
        return workPlanService.getAllWorkPlan();
    }

    @RequestMapping(value = "/workplan/user/{id}", method = RequestMethod.GET)
    public @ResponseBody List<WorkPlan> getWorkPlanForUser(@PathVariable long id) {
        log.info("Get work plan for user " + id);
        return workPlanService.getWorkPlanForUser(id);
    }
    
    @RequestMapping(value = "/workplan", method = RequestMethod.POST)
    public @ResponseBody ResponseStatus create(@RequestBody WorkPlan workPlan) {
        log.info("Create work plan.");
    	boolean result = workPlanService.create(workPlan);
        return result ? ResponseStatus.ADDED : ResponseStatus.ERROR;
    }
    
    @RequestMapping(value = "/workplan/{id}", method = RequestMethod.PUT)
    public @ResponseBody ResponseStatus update(@PathVariable long id, @RequestBody WorkPlan workPlan) {
        log.info("Update work plan with id " + id);
        boolean result = workPlanService.update(id, workPlan);
        return result ? ResponseStatus.UPDATED : ResponseStatus.ERROR;
    }
    
    @RequestMapping(value = "/workplan/{id}", method = RequestMethod.DELETE)
    public @ResponseBody ResponseStatus delete(@PathVariable long id) {
        log.info("Delete work plan with id " + id);
        boolean result = workPlanService.delete(id);
        return result ? ResponseStatus.DELETED : ResponseStatus.ERROR;
    }
}
